package csdn_2;

import java.io.*;

public class Copy_Stream {
    public static void main(String[] args) {
        String srcFilepath="D:\\javacode\\javacode\\IO_1\\mysql_1.jpg";//源路径
        String destFilepath="D:\\javacode\\javacode\\IO_1\\mysql_3.jpg";//拷贝路径

        BufferedInputStream bufferedInputStream=null;
        BufferedOutputStream bufferedOutputStream=null;

        byte[] buff = new byte[1024];//用于存放读取到的数据
        int readLen = 0;//实际读取到的字节数

        try {
            bufferedInputStream=new BufferedInputStream(new FileInputStream(srcFilepath));
            bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(destFilepath));

            while((readLen=bufferedInputStream.read(buff))!=-1){//当返回-1 时，就表示文件读取完毕
                bufferedOutputStream.write(buff,0,readLen);//将读取到的数据写入指定路径
            }
            System.out.println("拷贝0k~");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        finally {
            //关闭流, 这里注意，只需要关闭 BBufferedInputStream、BufferedOutputStream，
            // 因为底层会自动的去关闭节点流FileInputStream、FileOutputStream
            try {
                if(bufferedInputStream!=null) {
                    bufferedInputStream.close();
                }
                if(bufferedOutputStream!=null){
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
